import json
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

class Douyu:
    def __init__(self):
        self.url = 'https://www.douyu.com/directory/all'
        self.driver = webdriver.Edge()

    def parse_data(self):
        live_list = self.driver.find_elements(By.XPATH, '//*[@id="listAll"]/section[2]/div[2]/ul/li/div')
        print(len(live_list))
        data_list = []
        for item in live_list:
            temp = {}
            temp['title'] = item.find_element(By.XPATH, './div[2]/div[1]/a').text
            temp['peoples'] = item.find_element(By.XPATH, './div[1]/div/a/div/div/div/div[2]/span').text
            temp['owner'] = item.find_element(By.XPATH, './div[1]/div/a/div/div/div/div[1]/div').text
            temp['img'] = item.find_element(By.XPATH, './div[1]/picture/source[1]').get_attribute('srcset')
            data_list.append(temp)
        return data_list
    def save_data(self,data_list):
        for data in data_list:
            # with open("live_data.txt", 'a', encoding='utf8')as f:
            #     json.dump(data, f)
            print(data)
    def run(self):
        self.driver.get(self.url)
        self.driver.maximize_window()
        for i in range(3):
            self.driver.execute_script('window.scrollBy(0, 10000)')
            time.sleep(10)
        data_list = self.parse_data()
        self.parse_data()
        self.save_data(data_list)

if __name__ == '__main__':
    douyu = Douyu()
    try:
        douyu.run()
    except:
        print('有错')
    # with open('live_data.txt', 'r', encoding='utf8')as f:
    #     live = json.load(f)
    #     print(live)
